package com.hxb.library.utils;

import android.content.Context;
import android.os.Build;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.hxb.library.R;
import com.hxb.library.integration.AppManager;

/**
 * @author: HeXingBo
 * @date: 2023/3/10
 * @description：
 */
public class ToastUtils {
    private static Toast toast;

    private static Context mContext;

    public static void show(String text, int duration) {
        show(AppManager.appContext().getApplicationContext(), text, duration);
    }

    public static void show(String text) {
        show(AppManager.appContext().getApplicationContext(), text);
    }

    public static void show(int id, int duration) {
        show(AppManager.appContext().getApplicationContext(), id, duration);
    }

    public static void show(int id) {
        show(AppManager.appContext().getApplicationContext(), id);
    }

    public static void show(Context context, String text, int duration) {
        if (null == context)
            return;
        mContext = context.getApplicationContext();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
            if (null == toast) {
                toast = Toast.makeText(mContext, text, duration);
            } else {
                toast.setText(text);
                toast.setDuration(duration);
            }
            toast.show();
        } else {
            if (null != toast) {
                toast.cancel();
                toast = null;
            }
            toast = Toast.makeText(mContext, text, duration);
            toast.show();
        }
    }

    public static void show(Context context, String text) {
        if (null == context)
            return;
        mContext = context.getApplicationContext();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
            if (null == toast) {
                toast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT);
                toast = new Toast(context);
            } else {
                toast.setText(text);
                toast.setDuration(Toast.LENGTH_SHORT);
            }
            toast.setMargin(0,0);
            toast.show();
        } else {
            if (null != toast) {
                toast.cancel();
                toast = null;
            }
            toast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT);
            toast.setMargin(0,0);
            toast.show();
        }
    }

    public static void show(Context context, int id, int duration) {
        if (null == context)
            return;
        mContext = context.getApplicationContext();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
            if (null == toast) {
                toast = Toast.makeText(mContext, id, duration);
            } else {
                toast.setText(id);
                toast.setDuration(duration);
            }
            toast.show();

        } else {
            if (null != toast) {
                toast.cancel();
                toast = null;
            }
            toast = Toast.makeText(mContext, id, duration);
            toast.show();
        }

    }

    public static void show(Context context, int id) {
        if (null == context)
            return;
        mContext = context.getApplicationContext();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
            if (null == toast) {
                toast = Toast.makeText(mContext, id, Toast.LENGTH_SHORT);
            } else {
                toast.setText(id);
                toast.setDuration(Toast.LENGTH_SHORT);
            }
            toast.show();
        } else {
            if (null != toast) {
                toast.cancel();
                toast = null;
            }
            toast = Toast.makeText(mContext, id, Toast.LENGTH_SHORT);
            toast.show();
        }
    }

    public static View initView(String text) {
        View view = View.inflate(AppManager.appContext(), R.layout.layout_toast, null);
        TextView messageTextView = (TextView) view.findViewById(R.id.message);
        messageTextView.setText(text);
        return view;
    }
}
